/*
 * © Copyright 2011 Romain Marteau, Matthieu Totet
 * 
 *    This file is part of VineaToum.
 *
 *    VineaToum is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    VineaToum is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with VineaToum.  If not, see <http://www.gnu.org/licenses/>.
 *    
 */

package org.vineatoum.view.data;

import java.awt.BorderLayout;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JTextField;

import org.vineatoum.business.Parcelle;
import org.vineatoum.business.configuration.LieuDit;
import org.vineatoum.business.configuration.SoilType;
import org.vineatoum.model.abstraction.IVineaToumModel;
import org.vineatoum.view.utils.JPanelWrapped;

public class ParcelleDataPanel extends JPanelWrapped {
	private static final long serialVersionUID = 2962652253267605435L;
	private JTextField textFieldName;
	private JTextField textFieldCadastraNumber;
	private JComboBox comboBoxLieuDit;
//	JComboBox comboBoxCommune;
	private JComboBox comboBoxSoilType;
	private JSpinner spinnerSurface;

	/**
	 * Create the dialog.
	 * @param parcelle 
	 */
	public ParcelleDataPanel(IVineaToumModel model, Parcelle parcelle) {
		setBounds(100, 100, 450, 300);
		setLayout(new BorderLayout());
		{
			GridBagLayout gbl_panel = new GridBagLayout();
			gbl_panel.columnWidths = new int[] { 0, 0, 0, 0 };
			gbl_panel.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0 };
			gbl_panel.columnWeights = new double[] { 1.0, 1.0, 1.0,
					Double.MIN_VALUE };
			gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
					Double.MIN_VALUE };
			setLayout(gbl_panel);
			{
				JLabel lblNom = new JLabel("Nom");
				GridBagConstraints gbc_lblNom = new GridBagConstraints();
				gbc_lblNom.anchor = GridBagConstraints.EAST;
				gbc_lblNom.insets = new Insets(0, 0, 5, 5);
				gbc_lblNom.gridx = 0;
				gbc_lblNom.gridy = 0;
				add(lblNom, gbc_lblNom);
			}
			{
				setTextFieldName(new JTextField());
				getTextFieldName().setText(parcelle.getName());
				GridBagConstraints gbc_textField = new GridBagConstraints();
				gbc_textField.insets = new Insets(0, 0, 5, 5);
				gbc_textField.fill = GridBagConstraints.HORIZONTAL;
				gbc_textField.gridx = 1;
				gbc_textField.gridy = 0;
				add(getTextFieldName(), gbc_textField);
				getTextFieldName().setColumns(10);
			}
			{
				JLabel lblCadastralNumber = new JLabel(
						"Num\u00E9ro de cadastre");
				GridBagConstraints gbc_lblCadastralNumber = new GridBagConstraints();
				gbc_lblCadastralNumber.anchor = GridBagConstraints.EAST;
				gbc_lblCadastralNumber.insets = new Insets(0, 0, 5, 5);
				gbc_lblCadastralNumber.gridx = 0;
				gbc_lblCadastralNumber.gridy = 1;
				add(lblCadastralNumber, gbc_lblCadastralNumber);
			}
			{
				setTextFieldCadastraNumber(new JTextField());
				getTextFieldCadastraNumber().setText(parcelle.getCadastralNumber());
				GridBagConstraints gbc_textField_1 = new GridBagConstraints();
				gbc_textField_1.insets = new Insets(0, 0, 5, 5);
				gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
				gbc_textField_1.gridx = 1;
				gbc_textField_1.gridy = 1;
				add(getTextFieldCadastraNumber(), gbc_textField_1);
				getTextFieldCadastraNumber().setColumns(10);
			}
			{
				JLabel lblLieuDit = new JLabel("Lieu Dit");
				GridBagConstraints gbc_lblLieuDit = new GridBagConstraints();
				gbc_lblLieuDit.anchor = GridBagConstraints.EAST;
				gbc_lblLieuDit.insets = new Insets(0, 0, 5, 5);
				gbc_lblLieuDit.gridx = 0;
				gbc_lblLieuDit.gridy = 2;
				add(lblLieuDit, gbc_lblLieuDit);
			}
			{
				setComboBoxLieuDit(new JComboBox());
				List<LieuDit> data = new ArrayList<LieuDit>();
				for (Iterator<LieuDit> iterator = LieuDit.lieuDits.iterator(); iterator.hasNext();) {
					LieuDit lieuDit = (LieuDit) iterator.next();
					if(lieuDit.getName() != null && !lieuDit.getName().isEmpty()) {
						data.add(lieuDit);
					}
				}
				getComboBoxLieuDit().setModel(new DefaultComboBoxModel(data.toArray()));
				getComboBoxLieuDit().setSelectedItem(parcelle.getPlaceName());
				GridBagConstraints gbc_textField_2 = new GridBagConstraints();
				gbc_textField_2.insets = new Insets(0, 0, 5, 5);
				gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
				gbc_textField_2.gridx = 1;
				gbc_textField_2.gridy = 2;
				add(getComboBoxLieuDit(), gbc_textField_2);
			}
//			{
//				JLabel lblCommune = new JLabel("Commune");
//				GridBagConstraints gbc_lblCommune = new GridBagConstraints();
//				gbc_lblCommune.anchor = GridBagConstraints.EAST;
//				gbc_lblCommune.insets = new Insets(0, 0, 5, 5);
//				gbc_lblCommune.gridx = 0;
//				gbc_lblCommune.gridy = 3;
//				add(lblCommune, gbc_lblCommune);
//			}
//			{
//				comboBoxCommune = new JComboBox();
//				List<Commune> data = new ArrayList<Commune>();
//				for (Iterator<Commune> iterator = Commune.communes.iterator(); iterator.hasNext();) {
//					Commune commune = (Commune) iterator.next();
//					if(commune.getName() != null && !commune.getName().isEmpty()) {
//						data.add(commune);
//					}
//				}
//				comboBoxCommune.setModel(new DefaultComboBoxModel(data.toArray()));
//				comboBoxCommune.setSelectedItem(parcelle.getCommune());
//				
//				GridBagConstraints gbc_textField_3 = new GridBagConstraints();
//				gbc_textField_3.insets = new Insets(0, 0, 5, 5);
//				gbc_textField_3.fill = GridBagConstraints.HORIZONTAL;
//				gbc_textField_3.gridx = 1;
//				gbc_textField_3.gridy = 3;
//				add(comboBoxCommune, gbc_textField_3);
//			}
			{
				JLabel lblSurface = new JLabel("Surface");
				GridBagConstraints gbc_lblSurface = new GridBagConstraints();
				gbc_lblSurface.anchor = GridBagConstraints.EAST;
				gbc_lblSurface.insets = new Insets(0, 0, 5, 5);
				gbc_lblSurface.gridx = 0;
				gbc_lblSurface.gridy = 4;
				add(lblSurface, gbc_lblSurface);
			}
			{
				setSpinnerSurface(new JSpinner());
				getSpinnerSurface().setModel(new SpinnerNumberModel(new Float(0), new Float(0), new Float(99999), new Float(0.1)));
				getSpinnerSurface().setValue(parcelle.getSurfaceArea());
				GridBagConstraints gbc_textField_4 = new GridBagConstraints();
				gbc_textField_4.insets = new Insets(0, 0, 5, 5);
				gbc_textField_4.fill = GridBagConstraints.HORIZONTAL;
				gbc_textField_4.gridx = 1;
				gbc_textField_4.gridy = 4;
				add(getSpinnerSurface(), gbc_textField_4);
			}
			{
				JLabel lblTypeDeSol = new JLabel("Type de Sol");
				GridBagConstraints gbc_lblTypeDeSol = new GridBagConstraints();
				gbc_lblTypeDeSol.anchor = GridBagConstraints.EAST;
				gbc_lblTypeDeSol.insets = new Insets(0, 0, 5, 5);
				gbc_lblTypeDeSol.gridx = 0;
				gbc_lblTypeDeSol.gridy = 5;
				add(lblTypeDeSol, gbc_lblTypeDeSol);
			}
			{
				setComboBoxSoilType(new JComboBox());
				List<SoilType> data = new ArrayList<SoilType>();
				for (Iterator<SoilType> iterator = SoilType.soilTypes.iterator(); iterator
						.hasNext();) {
					SoilType it = (SoilType) iterator.next();
					if (it.getName() != null && !it.getName().isEmpty()) {
						data.add(it);
					}
				}
				getComboBoxSoilType().setModel(new DefaultComboBoxModel(data.toArray()));

				GridBagConstraints gbc_textField_5 = new GridBagConstraints();
				gbc_textField_5.insets = new Insets(0, 0, 5, 5);
				gbc_textField_5.fill = GridBagConstraints.HORIZONTAL;
				gbc_textField_5.gridx = 1;
				gbc_textField_5.gridy = 5;
				add(getComboBoxSoilType(), gbc_textField_5);
			}
			
			updateField(parcelle);
//			{
//				JLabel lblGpsLocation = new JLabel("Coordonn\u00E9es GPS");
//				GridBagConstraints gbc_lblCoordonnesGps = new GridBagConstraints();
//				gbc_lblCoordonnesGps.anchor = GridBagConstraints.EAST;
//				gbc_lblCoordonnesGps.insets = new Insets(0, 0, 0, 5);
//				gbc_lblCoordonnesGps.gridx = 0;
//				gbc_lblCoordonnesGps.gridy = 5;
//				add(lblGpsLocation, gbc_lblCoordonnesGps);
//			}
		}
	}
	
	public void updateField(Parcelle parcelle) {
		getTextFieldName().setText(parcelle.getName());
		getTextFieldCadastraNumber().setText(parcelle.getCadastralNumber());
		getComboBoxLieuDit().setSelectedItem(parcelle.getPlaceName());
		getComboBoxSoilType().setSelectedItem(parcelle.getSoilType());
		getSpinnerSurface().setValue(parcelle.getSurfaceArea());
	}

	@Override
	public void doWhenWrapperCancelClick() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doWhenWrapperOkClick() {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @param textFieldName the textFieldName to set
	 */
	public void setTextFieldName(JTextField textFieldName) {
		this.textFieldName = textFieldName;
	}

	/**
	 * @return the textFieldName
	 */
	public JTextField getTextFieldName() {
		return textFieldName;
	}

	/**
	 * @param textFieldCadastraNumber the textFieldCadastraNumber to set
	 */
	public void setTextFieldCadastraNumber(JTextField textFieldCadastraNumber) {
		this.textFieldCadastraNumber = textFieldCadastraNumber;
	}

	/**
	 * @return the textFieldCadastraNumber
	 */
	public JTextField getTextFieldCadastraNumber() {
		return textFieldCadastraNumber;
	}

	/**
	 * @param comboBoxLieuDit the comboBoxLieuDit to set
	 */
	public void setComboBoxLieuDit(JComboBox comboBoxLieuDit) {
		this.comboBoxLieuDit = comboBoxLieuDit;
	}

	/**
	 * @return the comboBoxLieuDit
	 */
	public JComboBox getComboBoxLieuDit() {
		return comboBoxLieuDit;
	}

	/**
	 * @param spinnerSurface the spinnerSurface to set
	 */
	public void setSpinnerSurface(JSpinner spinnerSurface) {
		this.spinnerSurface = spinnerSurface;
	}

	/**
	 * @return the spinnerSurface
	 */
	public JSpinner getSpinnerSurface() {
		return spinnerSurface;
	}

	/**
	 * @param comboBoxSoilType the comboBoxSoilType to set
	 */
	public void setComboBoxSoilType(JComboBox comboBoxSoilType) {
		this.comboBoxSoilType = comboBoxSoilType;
	}

	/**
	 * @return the comboBoxSoilType
	 */
	public JComboBox getComboBoxSoilType() {
		return comboBoxSoilType;
	}

}
